<?php
/**
 *
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2019/5/28 14:36
 * @copyright PanxSoft Inc.
 */

namespace App\Http\Controllers\App\V1;


use App\Http\Controllers\ApiController;
use App\Models\AdvertType;
use App\Models\SplashAdvert;
use Illuminate\Database\Eloquent\Collection;

class SplashAdvertController extends ApiController
{
    const ROUTES = [
        'index'       => 'app.v1.splash-advert.index',
        'indexInApp'  => 'app.v1.splash-advert.indexInApp',
    ];

    /**
     * 首屏广告列表
     *
     * @return array|\Illuminate\Http\Response
     */
    public function index()
    {
        $splash_adverts = $this->queryAdvertType(AdvertType::SPLASH_IDS);

        return $this->response(compact('splash_adverts'));
    }

    /**
     * 插屏广告
     *
     * @return array|\Illuminate\Http\Response
     */
    public function indexInApp()
    {
        $splash_adverts = $this->queryAdvertType(AdvertType::IN_APP_IDS);

        return $this->response(compact('splash_adverts'));
    }

    private function queryAdvertType(array $advert_type_ids): Collection
    {
        return (new SplashAdvert)
            ->oldest('sort_order')
            ->filterLimitDate()
            ->with('localeDescription.redirectable')
            ->whereIn('advert_type_id', $advert_type_ids)
            ->get();
    }
}